package com.suemi.missile.common.utils;

import java.io.File;
import java.io.IOException;

import com.suemi.missile.common.entity.Patent;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import java.util.*;
/**
 *
 * @author PIN
 * Patent文件处理
 *
 */


public class ExcelParser {

  public ExcelParser() {
    // TODO Auto-generated constructor stub
  }
  private List<Patent> listPatent = new LinkedList<Patent>();
  private Workbook workbook;

  private String getCellValue(Cell cell) {
    String str = null;
    switch (cell.getCellType()) {
      case Cell.CELL_TYPE_BLANK:
        str = "";
        break;
      case Cell.CELL_TYPE_BOOLEAN:
        str = String.valueOf(cell.getBooleanCellValue());
        break;
      case Cell.CELL_TYPE_FORMULA:
        str = String.valueOf(cell.getCellFormula());
        break;
      case Cell.CELL_TYPE_NUMERIC:
        str = String.valueOf(cell.getNumericCellValue());
        break;
      case Cell.CELL_TYPE_STRING:
        if(cell.getHyperlink() != null) str = cell.getHyperlink().getLabel();
        else str = String.valueOf(cell.getStringCellValue());
        break;
      default:
        str = null;
        break;
    }
    return str;
  }
  
  public void setUp(File path) throws InvalidFormatException, IOException {
    // 加载Patent文件,自动判断是HSSF还是XSSF  
    workbook = WorkbookFactory.create(path);  //"C:/Users/PIN/Desktop/test.xls"
  }
  public List<Patent> readPatent() throws InvalidFormatException, IOException {

    // 获取第一个工作目录,下标从0开始  
    Sheet sheet = workbook.getSheetAt(0);

    // 获取该工作目录最后一行的行数  
    int lastRowNum = sheet.getLastRowNum();

//      int lastRowNum = 10;
    for (int i = 1; i < lastRowNum; i++) {

      // 获取下标为i的行  
      Row row = sheet.getRow(i);
      Patent Patent = new Patent();
      Patent.setAllValue(row);
      listPatent.add(Patent);
      // 获取该行单元格个数  
    }
    return listPatent;
  }
  public static void main(String[] args) throws InvalidFormatException, IOException{
    ExcelParser docutil = new ExcelParser();
    File path = new File("C:/Users/PIN/Desktop/test.xls");
    docutil.setUp(path);
    docutil.readPatent();
    System.out.println("test ends");

  }
}
